﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Juece.Report
{
    public partial class 工贸产量 : System.Web.UI.Page
    {
        protected HSSFWorkbook hssfworkbook;
        protected double ytaojihua = 0;//月套管计划
        protected double yyoujihua = 0;//月油管计划
        protected double yyjiegujihua = 0;//月油管接箍计划
        protected double ytjiegujihua = 0;//月套管接箍计划
        protected double ybaohuqijihua = 0;//月保护器计划
        protected double ntaojihua = 0;//年套管计划
        protected double nyoujihua = 0;//年油管计划
        protected double nyjiegujihua = 0;//年油管接箍计划
        protected double ntjiegujihua = 0;//年套管接箍计划
        protected double nbaohuqijihua = 0;//年保护器计划

        protected double y2you = 0;//二分厂月油管
        protected double y5you = 0;//五分厂月油管
        protected double y1tao = 0;//一分厂月套管
        protected double y2tao = 0;//二分厂月套管
        protected int y3yjiegu = 0;//三分厂月油管接箍
        protected int y3tjiegu = 0;//三分厂月套管接箍
        protected int y4baohuqi = 0;//四分厂月保护器

        protected double n2you = 0;//二分厂年油管
        protected double n5you = 0;//五分厂年油管
        protected double n1tao = 0;//一分厂年套管
        protected double n2tao = 0;//二分厂年套管
        protected int n3yjiegu = 0;//三分厂年油管接箍
        protected int n3tjiegu = 0;//三分厂年套管接箍
        protected int n4baohuqi = 0;//四分厂年保护器

        protected double qn2you = 0;//去年二分厂年油管
        protected double qn5you = 0;//去年五分厂年油管
        protected double qn1tao = 0;//去年一分厂年套管
        protected double qn2tao = 0;//去年二分厂年套管
        protected int qn3yjiegu = 0;//去年三分厂年油管接箍
        protected int qn3tjiegu = 0;//去年三分厂年套管接箍
        protected int qn4baohuqi = 0;//去年四分厂年保护器

        protected double ytao = 0;//月套管
        protected double yyou = 0;//月油管
        protected int yyjiegu = 0;//月油管接箍
        protected int ytjiegu = 0;//月套管接箍
        protected int ybaohuqi = 0;//月保护器
        protected double ntao = 0;//年套管
        protected double nyou = 0;//年油管
        protected int nyjiegu = 0;//年油管接箍
        protected int ntjiegu = 0;//年套管接箍
        protected int nbaohuqi = 0;//年保护器
        protected double qntao = 0;//去年同期年套管
        protected double qnyou = 0;//去年同期年油管
        protected int qnyjiegu = 0;//去年同期年油管接箍
        protected int qntjiegu = 0;//去年同期年套管接箍
        protected int qnbaohuqi = 0;//去年同期年保护器
        protected double nyou同比增长 = 0;//年油管同比增长
        protected double ntao同比增长 = 0;//年套管同比增长
        protected int nyjiegu同比增长 = 0;//年油管接箍同比增长
        protected int ntjiegu同比增长 = 0;//年套管接箍同比增长
        protected int nbaohuqi同比增长 = 0;//年保护器年同比增长

        protected double m油管完成率 = 0;
        protected double m套管完成率 = 0;
        protected double m油管接箍完成率 = 0;
        protected double m套管接箍完成率 = 0;
        protected double m保护器完成率 = 0;

        protected double y油管完成率 = 0;
        protected double y套管完成率 = 0;
        protected double y油管接箍完成率 = 0;
        protected double y套管接箍完成率 = 0;
        protected double y保护器完成率 = 0;
        protected int year;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {             
                this.Month.SelectedIndex = DateTime.Today.Month-1;
                this.Year0.Text = DateTime.Today.Year.ToString();
            }
            //if (RiQi.Text == "")
            //{
            //    RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
            //    RiQi2.Text = DateTime.Today.Year.ToString() + "年" + (DateTime.Today.Month).ToString() + "月1日";
            //    RiQi3.Text = DateTime.Today.Year.ToString() + "年1月1日";
            //}
        }
        protected void WriteToFile()
        {
            DateTime d;
            
                d = DateTime.Today;
          
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + d.ToShortDateString() + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }

        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("/JueCe/模板/月主要产品产量表.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "陕西延长石油材料有限公司";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "陕西延长石油材料公司月产量表";
            hssfworkbook.SummaryInformation = si;
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {

            InitializeWorkbook();
            HSSFSheet sheet1 = hssfworkbook.GetSheet("工贸产量");
            Button_Query_Click(null, null);
            sheet1.GetRow(17).GetCell(5).SetCellValue(DateTime.Today.Year+"年"+DateTime.Today.Month+"月");

            sheet1.GetRow(20).GetCell(3).SetCellValue(yyoujihua);
            sheet1.GetRow(21).GetCell(3).SetCellValue(ytaojihua);
            sheet1.GetRow(22).GetCell(3).SetCellValue(yyjiegujihua);
            sheet1.GetRow(23).GetCell(3).SetCellValue(ytjiegujihua);
            sheet1.GetRow(24).GetCell(3).SetCellValue(ybaohuqijihua);

            sheet1.GetRow(20).GetCell(4).SetCellValue(yyou/1000);
            sheet1.GetRow(21).GetCell(4).SetCellValue(ytao /1000);
            sheet1.GetRow(22).GetCell(4).SetCellValue(yyjiegu);
            sheet1.GetRow(23).GetCell(4).SetCellValue(ytjiegu);
            sheet1.GetRow(24).GetCell(4).SetCellValue(ybaohuqi);

            sheet1.GetRow(20).GetCell(5).SetCellValue(((yyou / 1000d) / yyoujihua * 100).ToString("0.000"));
            sheet1.GetRow(21).GetCell(5).SetCellValue(((ytao / 1000d) / ytaojihua * 100).ToString("0.000"));
            sheet1.GetRow(22).GetCell(5).SetCellValue((yyjiegu / yyjiegujihua * 100).ToString("0.000"));
            sheet1.GetRow(23).GetCell(5).SetCellValue((ytjiegu / ytjiegujihua * 100).ToString("0.000"));
            sheet1.GetRow(24).GetCell(5).SetCellValue((ybaohuqi / ybaohuqijihua * 100).ToString("0.000"));

            sheet1.GetRow(20).GetCell(6).SetCellValue(nyoujihua);
            sheet1.GetRow(21).GetCell(6).SetCellValue(ntaojihua);
            sheet1.GetRow(22).GetCell(6).SetCellValue(nyjiegujihua);
            sheet1.GetRow(23).GetCell(6).SetCellValue(ntjiegujihua);
            sheet1.GetRow(24).GetCell(6).SetCellValue(nbaohuqijihua);

            sheet1.GetRow(20).GetCell(7).SetCellValue(nyou / 1000);
            sheet1.GetRow(21).GetCell(7).SetCellValue(ntao / 1000);
            sheet1.GetRow(22).GetCell(7).SetCellValue(nyjiegu);
            sheet1.GetRow(23).GetCell(7).SetCellValue(ntjiegu);
            sheet1.GetRow(24).GetCell(7).SetCellValue(nbaohuqi);

            sheet1.GetRow(20).GetCell(8).SetCellValue(((nyou / 1000) / nyoujihua * 100).ToString("0.000"));
            sheet1.GetRow(21).GetCell(8).SetCellValue(((ntao / 1000) / ntaojihua * 100).ToString("0.000"));
            sheet1.GetRow(22).GetCell(8).SetCellValue((nyjiegu / nyjiegujihua * 100).ToString("0.000"));
            sheet1.GetRow(23).GetCell(8).SetCellValue((ntjiegu / ntjiegujihua * 100).ToString("0.000"));
            sheet1.GetRow(24).GetCell(8).SetCellValue((nbaohuqi / nbaohuqijihua * 100).ToString("0.000"));

            sheet1.GetRow(20).GetCell(9).SetCellValue(qnyou);
            sheet1.GetRow(21).GetCell(9).SetCellValue(qntao);
            sheet1.GetRow(22).GetCell(9).SetCellValue(qnyjiegu);
            sheet1.GetRow(23).GetCell(9).SetCellValue(qntjiegu);
            sheet1.GetRow(24).GetCell(9).SetCellValue(qnbaohuqi);

            sheet1.GetRow(20).GetCell(10).SetCellValue(nyou同比增长);
            sheet1.GetRow(21).GetCell(10).SetCellValue(ntao同比增长);
            sheet1.GetRow(22).GetCell(10).SetCellValue(nyjiegu同比增长);
            sheet1.GetRow(23).GetCell(10).SetCellValue(ntjiegu同比增长);
            sheet1.GetRow(24).GetCell(10).SetCellValue(nbaohuqi同比增长);
            sheet1.GetRow(25).GetCell(8).SetCellValue("报出日期："+DateTime.Today.Year+"-"+DateTime.Today.Month+"-"+DateTime.Today.Day);
            WriteToFile();
            Response.End();
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            //  DateTime d1, d2, d3;
            //if (!DateTime.TryParse(this.RiQi.Text, out d1))
            //{
            //    this.Message.Text = "·请输入日期";
            //    return;
            //}
            //if (!DateTime.TryParse(this.RiQi2.Text, out d2))
            //{
            //    this.Message.Text = "·请输入累计日期";
            //    return;
            //}
            //if (!DateTime.TryParse(this.RiQi3.Text, out d3))
            //{
            //    this.Message.Text = "·请输入年累计日期";
            //    return;
            //}

          
            year = int.Parse(this.Year0.Text);

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            //获得月计划
            Cmd.CommandText = @"select (isnull(二分厂产品1,0)+isnull(五分厂产品1,0)) as 月油管计划,(isnull(一分厂产品2,0)+isnull(二分厂产品2,0)) as 月套管计划,isnull(三分厂产品1,0) as 月油管接箍,isnull(三分厂产品2,0) as 月套管接箍,isnull(四分厂产品2,0) as 月保护器 from 生产计划 where 年度='" + year + "' and 月份='" + this.Month.Text + "'";
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                yyoujihua = Reader.GetDouble(0);
                ytaojihua = Reader.GetDouble(1);
                yyjiegujihua = Reader.GetDouble(2);
                ytjiegujihua = Reader.GetDouble(3);
                ybaohuqijihua = Reader.GetDouble(4);

            }
            Reader.Close();
            //获得年计划
            Cmd.CommandText = @"select (isnull(二分厂产品1,0)+isnull(五分厂产品1,0)) as 月油管计划,(isnull(一分厂产品2,0)+isnull(二分厂产品2,0)) as 月套管计划,isnull(三分厂产品1,0) as 月油管接箍,isnull(三分厂产品2,0) as 月套管接箍,isnull(四分厂产品2,0) as 月保护器 from 生产计划 where 年度='" + year + "' and 月份=0";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                nyoujihua = Reader.GetDouble(0);
                ntaojihua = Reader.GetDouble(1);
                nyjiegujihua = Reader.GetDouble(2);
                ntjiegujihua = Reader.GetDouble(3);
                nbaohuqijihua = Reader.GetDouble(4);

            }
            Reader.Close();
            //二分厂月油管量          
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=2 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=2 and 产品类别ID=1 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y2you = Reader.GetDouble(0);
            }
            Reader.Close();
            //五分厂月油管
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=5 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=5 and 产品类别ID=1 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y5you = Reader.GetDouble(0);
            }
            //月油管
            yyou = y2you + y5you;
            Reader.Close();
            //一分厂月套管量
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=1 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=1 and 产品类别ID=2 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y1tao = Reader.GetDouble(0);
            }
            Reader.Close();
            //二分厂月套管量
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=2 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=2 and 产品类别ID=2 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y2tao = Reader.GetDouble(0);
            }
            Reader.Close();
            //月套管
            ytao = y1tao + y2tao;
            //三分厂月油管接箍量
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=3 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(磷化支数),0) from 生产进度3 where 产品类别ID=1 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y3yjiegu = Reader.GetInt32(0);
            }
            Reader.Close();
            //三分厂月套管接箍量
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=3 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(磷化支数),0) from 生产进度3 where 产品类别ID=2 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y3tjiegu = Reader.GetInt32(0);
            }
            Reader.Close();
            yyjiegu = int.Parse(y3yjiegu.ToString());
            ytjiegu = int.Parse(y3tjiegu.ToString());
            //四分厂月保护器
            Cmd.CommandText = @"
            declare @yc smalldatetime
            declare @ym smalldatetime
            select @yc=起始日期,@ym=截止日期 from 生产月设置
            where 分厂=4 and 年度='" + year + "' and 月份='" + this.Month.Text + "' select isnull(sum(入库),0) from 生产进度4 where 产品类别ID=2 and 日期 between @yc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                y4baohuqi = Reader.GetInt32(0);
            }
            Reader.Close();
            ybaohuqi = int.Parse(y4baohuqi.ToString());


            //二分厂年油管量          
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=2 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=2 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=2 and 产品类别ID=1 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n2you = Reader.GetDouble(0);
            }
            Reader.Close();
            //五分厂年油管
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=5 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=5 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=5 and 产品类别ID=1 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n5you = Reader.GetDouble(0);
            }
            //年油管
            nyou = n2you + n5you;
            Reader.Close();
            //一分厂年套管量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=1 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=1 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=1 and 产品类别ID=2 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n1tao = Reader.GetDouble(0);
            }
            Reader.Close();
            //二分厂年套管量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=2 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=2 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=2 and 产品类别ID=2 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n2tao = Reader.GetDouble(0);
            }
            Reader.Close();
            //年套管
            ntao = n1tao + n2tao;
            //三分厂年油管接箍量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=3 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=3 select isnull(sum(磷化支数),0) from 生产进度3 where  产品类别ID=1 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n3yjiegu = Reader.GetInt32(0);
            }
            Reader.Close();
            //三分厂年套管接箍量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=3 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=3 select isnull(sum(磷化支数),0) from 生产进度3 where  产品类别ID=2 and 日期 between @nc and @ym";

            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n3tjiegu = Reader.GetInt32(0);
            }
            Reader.Close();
            nyjiegu = int.Parse(n3yjiegu.ToString());
            ntjiegu = int.Parse(n3tjiegu.ToString());
            //四分厂年保护器
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + year + "' and 月份='1'and 分厂=4 select @ym=截止日期 from 生产月设置 where 年度='" + year + "' and 月份='" + this.Month.Text + "' and 分厂=4 select isnull(sum(入库),0) from 生产进度4 where 产品类别ID=2 and 日期 between @nc and @ym";

            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                n4baohuqi = Reader.GetInt32(0);
            }
            Reader.Close();
            nbaohuqi = int.Parse(n4baohuqi.ToString());

            //二分厂去年油管量          
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=2 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=2 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=2 and 产品类别ID=1 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn2you = Reader.GetDouble(0);
            }
            Reader.Close();
            //五分厂去年油管
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=5 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=5 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=5 and 产品类别ID=1 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn5you = Reader.GetDouble(0);
            }
            //去年油管
            qnyou = (qn2you + qn5you)/1000;
            Reader.Close();
            //一分厂去年套管量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=1 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=1 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=1 and 产品类别ID=2 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn1tao = Reader.GetDouble(0);
            }
            Reader.Close();
            //二分厂去年套管量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=2 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=2 select isnull(sum(入库重量),0) from 生产进度 where 分厂ID=2 and 产品类别ID=2 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn2tao = Reader.GetDouble(0);
            }
            Reader.Close();
            //去年套管
            qntao = (qn1tao + qn2tao)/1000;
            //三分厂去年油管接箍量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=3 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=3 select isnull(sum(磷化支数),0) from 生产进度3 where  产品类别ID=1 and 日期 between @nc and @ym";
            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn3yjiegu = Reader.GetInt32(0);
            }
            Reader.Close();
            //三分厂去年套管接箍量
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=3 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=3 select isnull(sum(磷化支数),0) from 生产进度3 where  产品类别ID=2 and 日期 between @nc and @ym";

            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn3tjiegu = Reader.GetInt32(0);
            }
            Reader.Close();
            qnyjiegu = int.Parse(qn3yjiegu.ToString());
            qntjiegu = int.Parse(qn3tjiegu.ToString());
            //四分厂去年保护器
            Cmd.CommandText = @"
            declare @nc smalldatetime
            declare @ym smalldatetime
            select @nc=起始日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='1'and 分厂=4 select @ym=截止日期 from 生产月设置 where 年度='" + (year - 1) + "' and 月份='" + this.Month.Text + "' and 分厂=4 select isnull(sum(入库),0) from 生产进度4 where 产品类别ID=2 and 日期 between @nc and @ym";

            Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                qn4baohuqi = Reader.GetInt32(0);
            }
            Reader.Close();
            qnbaohuqi = int.Parse(qn4baohuqi.ToString());
            if (qnyou == 0)
            {
                nyou同比增长 = 0;
            }
            else
            {
                nyou同比增长 = ((nyou / 1000 / qnyou) - 1) * 100;
            }
            if (qntao == 0)
            {
                ntao同比增长 = 0;
            }
            else
            {
                ntao同比增长 = ((ntao / 1000 / qntao) - 1) * 100;
            }

            nyjiegu同比增长 = nyjiegu - qnyjiegu;
            ntjiegu同比增长 = ntjiegu - qntjiegu;
            nbaohuqi同比增长 = nbaohuqi - qnbaohuqi;
            if (yyoujihua == 0)
            {
                m油管完成率 = 0;
            }
            else
            {
                m油管完成率 = ((yyou / 1000) / yyoujihua * 100);
            }
            if (ytaojihua == 0)
            {
                m套管完成率 = 0;
            }
            else
            {
                m套管完成率 = ((ytao / 1000) / ytaojihua * 100);
            }
            if (yyjiegujihua == 0)
            {
                m油管接箍完成率 = 0;
            }
            else
            {
                m油管接箍完成率 = (yyjiegu / yyjiegujihua * 100);
            }
            if (ytjiegujihua == 0)
            {
                m套管接箍完成率 = 0;
            }
            else
            {
                m套管接箍完成率 = (ytjiegu / ytjiegujihua * 100);
            }
            if (ybaohuqijihua == 0)
            {
                m保护器完成率 = 0;
            }
            else
            {
                m保护器完成率 = (ybaohuqi / ybaohuqijihua * 100);
            }
            //-------------------------------------------
            if (nyoujihua == 0)
            {
                y油管完成率 = 0;
            }
            else
            {
                y油管完成率 = ((nyou / 1000) / nyoujihua * 100);
            }
            if (ntaojihua == 0)
            {
                y套管完成率 = 0;
            }
            else
            {
                y套管完成率 = ((ntao / 1000) / ntaojihua * 100);
            }
            if (nyjiegujihua == 0)
            {
                y油管接箍完成率 = 0;
            }
            else
            {
                y油管接箍完成率 = (nyjiegu / nyjiegujihua * 100);
            }
            if (ntjiegujihua == 0)
            {
                y套管接箍完成率 = 0;
            }
            else
            {
                y套管接箍完成率 = (ntjiegu / ntjiegujihua * 100);
            }
            if (nbaohuqijihua == 0)
            {
                y保护器完成率 = 0;
            }
            else
            {
                y保护器完成率 = (nbaohuqi / nbaohuqijihua * 100);
            }

            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
        }
    }
}
